package es.ats.codington.spring.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventSignUpService;

/**
 * EventSinUp Controller
 *
 */
@Controller
public class EventsSignUpController {
	
	/**
	 * This controller receives the session created by the visitor and the event selected.
	 * The visitor is registered to event, checking if visitor 
	 * is not registered and if there are seats available.
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/RegisterforEvent.htm")
	public String registerEventController(HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session=request.getSession();
		Visitor visitor = (Visitor) session.getAttribute("visitor");
		int idVisitor = visitor.getIdVisitor();
		
		int idEvent=Integer.parseInt(request.getParameter("idEvent"));
		
		EventSignUp e=new EventSignUp();
		e.setIdEvent(idEvent);
		e.setIdVisitor(idVisitor);
		
		EventSignUpService es=new EventSignUpService();
		
		try{
			
			if(es.checkEventsofVisitor(e)==false){
				
				if(es.checkSeatsAvailable(e)==true){
					
					if(es.registerVisitorForNewEvent(e)==1){
						
					es.updateMinusSeatsAvailableEvent(e);
					
	    			return "redirect:/vieweventsofuser.htm";
		    		}
					else {
					session.setAttribute("error", "NOT registered");
					String actual=(String)session.getAttribute("actual");
					if (actual=="ShowAllEvents"){
			    	return "redirect:/ShowAllEvents.htm";
					}
					else {return "redirect:/ShowEventsByPlaceError.htm";}
					}
				}
				else{
		    		session.setAttribute("error", "Seats NOT available");
					String actual=(String)session.getAttribute("actual");
					if (actual=="ShowAllEvents"){
			    	return "redirect:/ShowAllEvents.htm";
					}
					else {return "redirect:/ShowEventsByPlaceError.htm";}
					
				}
			}
			else {
				session.setAttribute("error", "Already registered");
				String actual=(String)session.getAttribute("actual");
				if (actual=="ShowAllEvents"){
	    		return "redirect:/ShowAllEvents.htm";
				}
				else {return "redirect:/ShowEventsByPlaceError.htm";}
			}
			
		}
			 catch(Exception eee){
				 return eee.getMessage();	
			    }
			           
		}

	/**
	 * This controller receives the session created by the visitor and the event selected.
	 * The visitor is unregistered of the event.
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/UnregisterforEvent.htm")
	public String unregisterEventController(HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session=request.getSession();
		Visitor visitor = (Visitor) session.getAttribute("visitor");
		int idVisitor = visitor.getIdVisitor();
		
		int idEvent=Integer.parseInt(request.getParameter("idEvent"));
		
		EventSignUp e=new EventSignUp();
		e.setIdEvent(idEvent);
		e.setIdVisitor(idVisitor);
		
		EventSignUpService es=new EventSignUpService();
		
		try{
					if(es.unregisterVisitorForEvent(e)==1){
					es.updateSumSeatsAvailableEvent(e);

	    			return "redirect:/vieweventsofuser.htm";
		    		}
					else {
						session.setAttribute("error", "NOT unregistered");
	    			return "redirect:/vieweventsofuser.htm";
					}

		}
			 catch(Exception eee){
				 return eee.getMessage();	
			    }
			           
		}
	
}



		
